package ualrcdg.server.validators;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

/**
 * Implementation for the CDG "Time" rule validation. See CDG XML Specifications: Game Descriptor Files. 
 */
public class TimeRule extends AbstractRuleValidator
{
	private static final TimeRule instance = new TimeRule( );
	private static final String rule = "time";
	private static final String className = "TimeRule";
	
	/**
	 * Private nullary constructor
	 */
	private TimeRule( )
	{
	}

	/**
	 * @return the singleton instance of this class
	 */
	@Override
	public TimeRule getInstance( )
	{
		return instance;
	}
	
	/*
	 * @see ualrcdg.server.validators.AbstractRuleValidator#validateRule(java.lang.String, java.util.HashMap, java.io.InputStream)
	 */
	@Override
	public boolean validateRule( String rule, HashMap< String, String > parameters, InputStream input )
	{
		if( rule == null || parameters == null || input == null )
		{
			throw new NullPointerException( );
		}
		
		if( !TimeRule.rule.equals( rule ) )
		{
			return false;
		}
		
		// LATER Complete rule validation implementation
		try
		{
			int length = input.available( );
			byte[] buffer = new byte[length];
			if( input.read( buffer ) != length )
			{
				throw new IOException( className + ": unable to read stream contents." );
			}
			System.out.println( className + ": checking " + parameters.toString( ) + " against {" + new String( buffer ) +"}" );
		}
		catch( IOException e )
		{
			e.printStackTrace();
			return false;
		}
		
		return false;
	}
}
